package org.cid54.bukkit.command;

import org.bukkit.Location;
import org.bukkit.entity.CreatureType;
import org.bukkit.event.player.PlayerChatEvent;
import org.cid54.bukkit.Cid54PlayerListener;
import org.cid54.bukkit.command.util.PlayerCommand;

/**
 *
 * @author fgo
 */
public class SpawnCommand implements PlayerCommand {

    public boolean execute(String command, PlayerChatEvent event, Cid54PlayerListener listener) {

        String[] commands = command.split(" ");

        if (commands.length > 1) {

            String argument1 = commands[1];

            if (argument1.toLowerCase().equals("list")) {

                for (CreatureType type : CreatureType.values()) {

                    event.getPlayer().sendMessage(type.name());

                }

            } else {

                CreatureType type = null;

                for (CreatureType creature : CreatureType.values()) {

                    if (creature.getName().toLowerCase().equals(commands[1].toLowerCase().trim())) {
                        type = creature;
                    }

                }

                int amount = 1;

                if (commands.length > 2) {
                    amount = Integer.parseInt(commands[2]);
                }

                Location loc = Cid54PlayerListener.cloneLocation(event.getPlayer().getLocation());

                loc.setX(loc.getX() + 3D);

                for (int i = 0; i < amount; i++) {

                    event.getPlayer().getWorld().spawnCreature(loc, type);

                }

            }
            return true;
        }

        return false;
    }

    public String getCommand() {
        return "spawn";
    }

    public String getHelpText() {
        return "spawn list | spawn <creatureType> <optional: amount>. Spawns a creature 3 block to the north of the player.";
    }
}
